package org.ogv.usada.states;

import org.ogv.CAssetManager.AssetDeclaration;
import org.ogv.OGV;
import org.ogv.statemanager.StateMgr;
import org.ogv.usada.models.GP;

import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public class LoadingScreen extends ScreenState {

	private AssetDeclaration _prevAssets;
	private AssetDeclaration _nextAssets;
	private int _nextStateId;
	private boolean _isReverse;
	
	public LoadingScreen(StateMgr sm, SpriteBatch sb, AssetDeclaration prevAssets, AssetDeclaration nextAssets, int nextStateId) {
		super(sb);
		setStateMgr(sm);
		_prevAssets = prevAssets;
		_nextAssets = nextAssets;
		_nextStateId = nextStateId;
	}

	@Override
	public void init() {
		//Doors
		initializeDoors();
		
		//not reversing
		_isReverse = false;
		
		//load next state assets, unload previous state's
		if(_nextAssets != null) OGV.asset.load(_nextAssets);
		if(_prevAssets != null) OGV.asset.unload(_prevAssets);
	}
	
	@Override
	public void resume() {
		//going reverse
		_isReverse = true;
		
		//load prev state assets, unload next state's
		if(_prevAssets != null) OGV.asset.load(_prevAssets);
		if(_nextAssets != null) OGV.asset.unload(_nextAssets);
	}
	
	@Override
	public void run(float elapsedTime) {
		if(!OGV.asset.update()) {
			//Resource is still loading ..
			//Draw doors
			_sb.begin();
			_sb.draw(_doorTexL, 0, 0);
			_sb.draw(_doorTexR, GP.$().GAME_WIDTH/2, 0);
			_sb.end();
		} else {
			//loading done, change state
			if(_isReverse) 
				getStateMgr().requestPopState();
			else 
				getStateMgr().requestPushState(_nextStateId);
		}
	}
}
